<template>
	<view class="flex-direction flex flex-center">
		<image src="../../static/icos/default-face.png" mode="" class="top-img"></image>
		<form>
			<view class="cu-form-group">
				<view class="title">账号</view>
				<input placeholder="请输入你的账号" v-model="phone"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">密码</view>
				<input placeholder="请输入你的密码" v-model="password"></input>
			</view>
			<button class="cu-btn bg-grey lg button" role="button" aria-disabled="false" @click="toLogin()">登录</button>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: "",
				password: ""
			};
		},
		methods: {
			toLogin() {
				const testDB = wx.cloud.database({
					env: 'test-demo-tihv0'
				})
				testDB.collection('test').where({
					phone: this.phone,
					password: this.password
				}).get().then(res => {
					// res.data 包含该记录的数据
					if (res.data && res.data.length > 0) {
						uni.showToast({
							title: '登录成功',
							duration: 1000
						});
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/my/my'
							});
						}, 1500)
					} else {
						uni.showToast({
							title: '账号或密码错误',
							duration: 2000
						});
					}
				}).catch(err => {
					console.log(err)
				})
			}
		}

	}
</script>

<style lang="scss" scoped>
	.top-img {
		width: 128upx;
		height: 128upx;
		margin: 20upx 0;
	}

	.button {
		width: 80%;
		margin: 20upx 10% 0;
	}
</style>
